package com.botao.service;

import com.botao.dao.ClassesDao;
import com.botao.dao.StudentDao;
import com.botao.pojo.Classes;
import com.botao.pojo.Page;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ClassesServiceImpl implements ClassesService {
    static Logger logger = Logger.getLogger(StudentServiceImpl.class);

    @Autowired
    @Qualifier("mybatisClassesDao")
    ClassesDao classesDao;
    @Autowired
    @Qualifier("mybatisStudentDao")
    StudentDao studentDao;
    @Override
    public void save(Classes classes) {
        classesDao.save(classes);
    }

    @Override
    public Page<Classes> listClasses(Long currentPage, Long pageSize) {
        List<Classes> classes = classesDao.listClasses(currentPage, pageSize);
        Long count = classesDao.count();
        Long totalPage = (count + pageSize - 1) / pageSize;
        return new Page<Classes>(classes,currentPage,pageSize,totalPage,count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(int id) {
        classesDao.delete(id);
        int deleteStudentCounts = studentDao.deleteStudentByClassesId(id);
        logger.info("删除学生数"+deleteStudentCounts);
        return true;
    }

    @Override
    public Classes getOne(int id) {
        return classesDao.getOne(id);
    }

    @Override
    public void update(Classes classes) {
        classesDao.update(classes);
    }

    @Override
    public List<Classes> getAllClasses() {
        return classesDao.getAllClasses();
    }
}
